package com.bestcem.xm.txtanls.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import com.bestcem.xm.common.core.constant.Constants;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.txtanls.dao.HiddenKeywordDao;
import com.bestcem.xm.txtanls.dao.mapper.HiddenKeywordMapper;
import com.bestcem.xm.txtanls.entity.mysql.HiddenKeywordEntity;
import com.bestcem.xm.txtanls.entity.pojo.HiddenKeywordDO;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 关键词相关DAO实现类
 *
 * @author jie.yao <jie.yao@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/27 14:17
 */
@Repository
public class HiddenKeywordDaoImpl implements HiddenKeywordDao {

    @Resource
    private HiddenKeywordMapper hiddenKeywordMapper;

    @Override
    public HiddenKeywordDO findHiddenKeywords(String projectId) {
        List<HiddenKeywordEntity> hiddenKeywordEntityList = hiddenKeywordMapper.listById(projectId);
        return toDO(projectId, hiddenKeywordEntityList);
    }

    @Override
    public List<String> listHiddenKeywords(String projectId) {
        List<HiddenKeywordEntity> hiddenKeywordEntityList = hiddenKeywordMapper.listById(projectId);
        if (CollUtil.isEmpty(hiddenKeywordEntityList)) {
            return new ArrayList<>();
        }
        return hiddenKeywordEntityList.stream().filter(item -> Validator.isNotEmpty(item.getKeyword()))
                .map(HiddenKeywordEntity::getKeyword).collect(Collectors.toList());
    }

    @Override
    public boolean addKeywords(String projectId, String keyword) {
        List<HiddenKeywordEntity> hiddenKeywordEntities = hiddenKeywordMapper.listById(projectId);
        String mongoId = new ObjectId().toString();
        if (CollUtil.isNotEmpty(hiddenKeywordEntities)) {
            mongoId = hiddenKeywordEntities.get(0).getMongoId();
        }
        HiddenKeywordEntity entity = new HiddenKeywordEntity();
        entity.setHiddenKeywordId(new ObjectId().toHexString());
        entity.setProjectId(projectId);
        entity.setKeyword(keyword);
        entity.setSample(false);

        entity.setMongoId(mongoId);
        entity.setCreateTime(DateUtil.getCommonDate());
        entity.setUpdateTime(DateUtil.getCommonDate());
        return hiddenKeywordMapper.insert(entity) > Constants.INT_ZERO;
    }

    @Override
    public boolean deleteKeywords(String projectId, String keyword) {
        return hiddenKeywordMapper.deleteByProjectIdAndKeyword(projectId, keyword) > Constants.INT_ZERO;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean updateKeywords(String projectId, List<String> keywordList) {
        // 根据id删除全部keyword
        hiddenKeywordMapper.deleteByProjectId(projectId);
        // 批量插入keyword
        List<HiddenKeywordEntity> hiddenKeywordEntityList = keywordList.stream().map(keyword -> {
            HiddenKeywordEntity entity = new HiddenKeywordEntity();
            entity.setHiddenKeywordId(new ObjectId().toHexString());
            entity.setOperatorId(projectId);
            entity.setKeyword(keyword);
            entity.setSample(false);
            entity.setMongoId(new ObjectId().toString());
            entity.setUpdateTime(DateUtil.getCommonDate());
            entity.setCreateTime(DateUtil.getCommonDate());
            return entity;
        }).collect(Collectors.toList());
        return hiddenKeywordMapper.insertKeywords(hiddenKeywordEntityList) > Constants.INT_ZERO;
    }

    @Override
    public boolean deleteProjectAllHidden(String projectId) {
        return hiddenKeywordMapper.deleteByProjectId(projectId) > Constants.INT_ZERO;
    }

    /**
     * entity to DO
     *
     * @param projectId               项目id
     * @param hiddenKeywordEntityList entityList
     * @return DO
     */
    private HiddenKeywordDO toDO(String projectId, List<HiddenKeywordEntity> hiddenKeywordEntityList) {
        HiddenKeywordDO hiddenKeywordDO = new HiddenKeywordDO();
        hiddenKeywordDO.setProjectId(projectId);
        List<String> keywords = hiddenKeywordEntityList.stream().map(HiddenKeywordEntity::getKeyword).collect(Collectors.toList());
        hiddenKeywordDO.setKeywordList(keywords);
        return hiddenKeywordDO;

    }
}
